package com.dahua.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dahua.R;
import com.dahua.bean.ScanResult;
import com.dahua.ui.main.ScanPrepareUploadAct;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 */
public class GridAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<ScanResult> nearbyInfoList;
    ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>();
    private Context context;

    public GridAdapter(Context context, ArrayList<HashMap<String, Object>> item) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.item = item;
    }

    public void setNearbyInfoList(List<ScanResult> nearbyInfoList) {
        this.nearbyInfoList = nearbyInfoList;
    }

    @Override
    public int getCount() {
        return  item.size();
    }

    @Override
    public Object getItem(int position) {
        return nearbyInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
            convertView = inflater.inflate(R.layout.gv_list, parent, false);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.tv_name);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final HashMap<String, Object> map  = item.get(position);

        if(!"".equals(map.get("name").toString())){
            holder.tv.setText(map.get("name").toString());
            holder.iv.setImageResource((Integer) map.get("img"));
        }
        return convertView;
    }

    public static class ViewHolder {
        ImageView iv;
        TextView tv;
    }


}
